<?php

namespace App\Http\Controllers\Bend;

use App\Logics\AccountLogic;
use App\Service\Captcha;
use App\Service\CNNickname;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\Crypt;
use App\Models\User;
use Illuminate\Support\Facades\Redirect;

class AccountController extends BaseController
{
    // 个人账户

    // 初始化
    public function initialize()
    {
        parent::initialize();
    }


    // 登录：手机号登录（如果没有注册 则自动注册）（pc端使用：手机号+密码+验证码）
    public function onLogin()
    {
        $admin = new AccountLogic();
        $user  = $admin->mobileLogin();
        if($user === false){
            return $this->error($admin->getError());
        }

        // 保存session
        $this->optLogic()->request()->session()->put('organ_user', $user);
        Cookie::queue(Cookie::make('organ_user', json_encode($user)));
        //Cookie::queue('user', json_encode($user));
        //cookie('user', json_encode($user));

        // 返回
        $token  = Crypt::encryptString(json_encode($user, JSON_UNESCAPED_UNICODE));
        $result = ['token' => $token, 'user' => $user,];

        return $this->success($result, '登录成功！');
    }


    // 验证码（在公共部分调用，这里不用）
    // 参考文档 https://blog.csdn.net/qq78442761/article/details/125796778
    // http://www.slh.com/mobile/account/captcha
    /*public function captcha()
    {
        // 展示验证码
        $image = Captcha::create("math", true);

        // 获取验证码的img作为图片显示
        return $this->success($image);
    }*/

    // 生成二维码验证码
    public function code()
    {
        $option = $this->optLogic();
        $size   = $option->getData('size', 20, 'intval'); // 字体大小
        $name   = $option->getData('name', 'organ_code', 'trim');
        $num    = $option->getData('num', 4, 'intval');
        $width  = $option->getData('width', 90, 'intval');
        $height = $option->getData('height', 32, 'intval');

        // 生成验证码
        $captcha = new Captcha();
        $image   = $captcha->makeCode($name, $num, $size, $width, $height);

        //return response($image, 200, ['Content-Length' => strlen($image)])->contentType('image/png');

        return $image;
    }

    // 登录
    public function login()
    {
        return $this->dispaly('account.login', []);
    }

    // 阿里云验证码
    public function aliCaptcha()
    {
        $logic = new AccountLogic();
        $res   = $logic->onAliCaptcha();
        if($res === false){
            return $this->error($logic->getError());
        }

        return $this->success($res);
    }

    // 退出
    public function logout()
    {
        //Cookie::queue(Cookie::make('organ_user', json_encode([])));
        //return $this->jumpLogin();

        // 删除cookie后需要重新定向
        $cookie = Cookie::forget('organ_user');
        $url    = Url('bend/account/login');

        return Redirect::secure($url)->withCookie($cookie);
    }

    // 生成token
    public function token()
    {
        $uid = $this->getData('uid');
        $this->throwIf(empty($uid), '获取指定用户的token！');

        $userDb = new User();
        $field  = ['id', 'username', 'email', 'status', 'realname', 'mobile',];
        $info   = $userDb->where(['id' => $uid])->select($field)->first(); // ->recycle()
        $this->throwIf(empty($info), '用户不存在！！');
        $this->throwIf($info->status === 0, '用户被禁用！！');

        // 新版本
        $info->ctime = LARAVEL_TIME;

        $info   = $info->toArray();
        $token  = Crypt::encryptString(json_encode($info, JSON_UNESCAPED_UNICODE));
        $result = ['token' => $token, 'user' => $info,];

        return $this->success($result);
    }

    // 随机生成昵称
    public function nick()
    {
        $nickname = (new CNNickname())->make();

        return $this->success($nickname, '获取随机昵称成功！');
    }
}


